//
//  LLForgetPasswordViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/11.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLForgetPasswordViewController.h"

@interface LLForgetPasswordViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) UIImageView *logoImgView;
@property (strong, nonatomic) UITextField *phoneTextField;
@property (strong, nonatomic) UITextField *passwordTextField;
@property (strong, nonatomic) LLButton *handlePwdTextFieldBtn;
@property (strong, nonatomic) UITextField *codeTextField;
@property (strong, nonatomic) LLButton *codeBtn;
@property (strong, nonatomic) LLButton *changePwdBtn;
@end

@implementation LLForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.backgroundColor = UIColor.whiteColor;
}

- (void)initUI {
    // logo
    CGFloat top = LLScreenWidth < 375 ? 30 : 50;
    CGFloat left = LLScreenWidth < 375 ? 40 : 50;
    CGFloat logoImgViewW = 83;
    UIImageView *topLogo = [UIImageView new];
    topLogo.frame = CGRectMake((LLScreenWidth - logoImgViewW)/2.0, 0, logoImgViewW, logoImgViewW);
    topLogo.image = [UIImage imageNamed:@"底 拷贝"];
    [self.view addSubview:topLogo];
    topLogo.hidden = YES;
    
    _logoImgView = [UIImageView new];
    _logoImgView.frame = CGRectMake((LLScreenWidth - logoImgViewW)/2.0, topLogo.bottom-17, logoImgViewW, logoImgViewW);
    _logoImgView.image = [UIImage imageNamed:@"logo倒影"];
    [self.view addSubview:_logoImgView];
    _logoImgView.hidden = YES;
    
    CGFloat fieldH = LLScreenWidth < 375 ? 40 : 50;
    CGFloat phoneTextFieldTopMargin =  LLScreenWidth < 375 ? 10 : 20;
    // 账号输入框
    _phoneTextField = [UITextField new];
    _phoneTextField.frame = CGRectMake(left, _logoImgView.bottom + phoneTextFieldTopMargin, LLScreenWidth - left * 2, fieldH);
    _phoneTextField.placeholder = @"请输入手机号码";
    
    UIView *phoneLeftView = UIView.new;
    phoneLeftView.size = CGSizeMake(fieldH-10, fieldH);
    UIImageView *phoneImgView = [UIImageView new];
    phoneImgView.frame = CGRectMake(10, (fieldH-17)/2.0, 12, 17);
    phoneImgView.image = [UIImage imageNamed:@"手机icon"];
    [phoneLeftView addSubview:phoneImgView];
    
    _phoneTextField.leftView = phoneLeftView;
    _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    _phoneTextField.tag = 0;
    _phoneTextField.font = [UIFont systemFontOfSize:15];
    _phoneTextField.delegate = self;
    _phoneTextField.tintColor = LLColor(225, 0, 0);
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTextField.inputAccessoryView = [UIView new];
    _phoneTextField.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
    [_phoneTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_phoneTextField];
    CALayer *line1 = [CALayer layer];
    line1.frame = CGRectMake(left, _phoneTextField.bottom, LLScreenWidth - left * 2, 0.5);
    line1.backgroundColor = LLColorRGB(153).CGColor;
    [self.view.layer addSublayer:line1];
    
    //操作密码输入框的按钮
    _handlePwdTextFieldBtn = [LLButton new];
    _handlePwdTextFieldBtn.frame = CGRectMake(LLScreenWidth-40-left, _phoneTextField.bottom+10, 40, fieldH);
    [_handlePwdTextFieldBtn setImage:[UIImage imageNamed:@"睁眼"] forState:0];
    [_handlePwdTextFieldBtn setImage:[UIImage imageNamed:@"闭眼"] forState:UIControlStateSelected];
    _handlePwdTextFieldBtn.selected = YES;
    [self.view addSubview:_handlePwdTextFieldBtn];
    [_handlePwdTextFieldBtn addTarget:self action:@selector(handlePwdTextFieldBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 密码输入框
    _passwordTextField = [UITextField new];
    _passwordTextField.frame = CGRectMake(left, _phoneTextField.bottom + 10, _handlePwdTextFieldBtn.left-left, fieldH);
    _passwordTextField.placeholder = @"请输入密码";
    
    UIView *pwdLeftView = UIView.new;
    pwdLeftView.size = CGSizeMake(fieldH-10, fieldH);
    UIImageView *pwdImgView = [UIImageView new];
    pwdImgView.frame = CGRectMake(10, (fieldH-15)/2.0, 13, 15);
    pwdImgView.image = [UIImage imageNamed:@"密码icon"];
    [pwdLeftView addSubview:pwdImgView];
    
    _passwordTextField.leftView = pwdLeftView;
    _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
    _passwordTextField.tag = 1;
    _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwordTextField.returnKeyType = UIReturnKeyDone;
    _passwordTextField.delegate = self;
    _passwordTextField.tintColor = LLColor(225, 0, 0);
    _passwordTextField.inputAccessoryView = [UIView new];
    _passwordTextField.secureTextEntry = YES;
    _passwordTextField.keyboardType = UIKeyboardTypeASCIICapable;
    _passwordTextField.font = [UIFont systemFontOfSize:15];
    [_passwordTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_passwordTextField];
    CALayer *line2 = [CALayer layer];
    line2.frame = CGRectMake(left, _passwordTextField.bottom, LLScreenWidth - left * 2, 0.5);
    line2.backgroundColor = LLColorRGB(153).CGColor;
    [self.view.layer addSublayer:line2];
    
    // 验证码按钮
    _codeBtn = [LLInitBaseViewTool buttonWithTitle:@"获取验证码" image:nil titleColor:LLColor(225, 0, 0) font:15];
    _codeBtn.frame = CGRectMake(LLScreenWidth-100-left, _passwordTextField.bottom+30, 100, fieldH);
    _codeBtn.layer.cornerRadius = 8;
    _codeBtn.layer.borderColor = LLColor(225, 0, 0).CGColor;
    _codeBtn.layer.borderWidth = 1;
    [_codeBtn addTarget:self action:@selector(codeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codeBtn];
    
    //验证码输入框
   _codeTextField = [UITextField new];
    _codeTextField.frame = CGRectMake(left, _codeBtn.top, _codeBtn.left-left-15, fieldH);
    _codeTextField.layer.borderWidth = 1;
    _codeTextField.layer.borderColor = LLColorRGB(171).CGColor;
    _codeTextField.layer.cornerRadius = 8;
    _codeTextField.placeholder = @"请输入验证码";
    _codeTextField.tag = 2;
    _codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTextField.returnKeyType = UIReturnKeyDone;
    _codeTextField.delegate = self;
    _codeTextField.tintColor = LLColor(225, 0, 0);
    _codeTextField.inputAccessoryView = [UIView new];
    _codeTextField.textAlignment = NSTextAlignmentCenter;
    _codeTextField.keyboardType = UIKeyboardTypeASCIICapable;
    _codeTextField.font = [UIFont systemFontOfSize:15];
    [_codeTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_codeTextField];
    
    // 修改密码按钮
    CGFloat btnY = LLScreenHeight < 375 ? LLScreenHeight-self.navH-90 : LLScreenHeight-self.navH-120;
    _changePwdBtn = [LLInitBaseViewTool buttonWithTitle:@"修改密码" image:nil titleColor:[UIColor whiteColor] font:17];
    _changePwdBtn.frame = CGRectMake(left, btnY, LLScreenWidth-left*2, 38);
    [_changePwdBtn setBackgroundColor:LLColor(225, 0, 0)];
    _changePwdBtn.layer.cornerRadius = 19;
    [_changePwdBtn addTarget:self action:@selector(changePwdBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_changePwdBtn];
}

#pragma mark - private method

- (void)handlePwdTextFieldBtnClick:(LLButton *)btn {
    _handlePwdTextFieldBtn.selected = !_handlePwdTextFieldBtn.isSelected;
    [self.passwordTextField setSecureTextEntry:!self.passwordTextField.secureTextEntry];
    NSString *tempStr = _passwordTextField.text;
    _passwordTextField.text = nil;
    _passwordTextField.text = tempStr;
}

- (void)codeBtnClick:(UIButton *)button {
    if (self.phoneTextField.text.length == 0) {
        [self showSVProgressHUDInfoWithsStatus:@"您还未输入手机号~"];
        [SVProgressHUD dismissWithDelay:1];
    }else{
        [self startTime:button];
        [self getCode];
    }
}

- (void)changePwdBtnClick {
    if (self.phoneTextField.text.length == 0) {
        [self showSVProgressHUDInfoWithsStatus:@"您还未输入手机号~"];
        [SVProgressHUD dismissWithDelay:1];
    }else if (self.passwordTextField.text.length == 0){
        [self showSVProgressHUDInfoWithsStatus:@"您还未输入密码~"];
        [SVProgressHUD dismissWithDelay:1];
    }else if (self.codeTextField.text.length == 0){
        [self showSVProgressHUDInfoWithsStatus:@"您还未输入验证码~"];
        [SVProgressHUD dismissWithDelay:1];
    }else{
        [self sendModityQuest];
    }
}
- (void)textFieldChange:(UITextField *)textField {
//    if (textField.markedTextRange == nil) {
//        if (textField.tag == 0) {
//            if (textField.text.length >= 11) {
//                textField.text = [textField.text substringToIndex:11];
//            }
//            self.phone = textField.text;
//            if (textField.text.length < 11) {
//                _passwordTextField.text = @"";
//                self.pwd = _passwordTextField.text;
//            }
//        } else {
//            if (textField.text.length >= 16) {
//                textField.text = [textField.text substringToIndex:16];
//            }
//            self.pwd = textField.text;
//        }
//        BOOL isPwd = self.pwd.length >= 6;
//        if (self.phone.length == 11 && isPwd) {
//            _loginBtn.enabled = YES;
//            _loginBtn.backgroundColor = LLMainColor;
//        } else {
//            _loginBtn.enabled = NO;
//            _loginBtn.backgroundColor = LLMainDisabledColor;
//        }
//    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return YES;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark====获取验证码
-(void)getCode{
    NSDictionary *dic = @{@"edu_id":@"1",@"mobile":self.phoneTextField.text,@"type":@"forget"};
    
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        [self showSVProgressHUDInfoWithsStatus:@"验证码已发送~"];
        [SVProgressHUD dismissWithDelay:1];
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        
    }];
}
#pragma mark====修改密码
-(void)sendModityQuest{
    NSDictionary *dic = @{@"edu_id":@"6",@"mobile":self.phoneTextField.text,@"pwd":self.passwordTextField.text,@"code":self.codeTextField.text};
    
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
       
        [self showSVProgressHUDInfoWithsStatus:@"修改成功~"];
        [SVProgressHUD dismissWithDelay:1];
        [self performSelector:@selector(didClickLeftNavAction) withObject:nil afterDelay:1];
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        
    }];
}
/**
 倒计时
 
 @param _l_timeButton 按钮
 */
-(void)startTime:(UIButton*)_l_timeButton{
    __block int timeout=30; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_l_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                _l_timeButton.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 100;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [_l_timeButton setTitle:[NSString stringWithFormat:@"稍等%@秒",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                _l_timeButton.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
@end
